"""
货物相关模型
"""
from tortoise import fields
from .base import BaseModel


class CargoCategory(BaseModel):
    """货物分类模型"""
    
    name = fields.CharField(max_length=100, unique=True, description="分类名称")
    code = fields.CharField(max_length=50, unique=True, description="分类代码")
    description = fields.TextField(null=True, description="分类描述")
    parent = fields.ForeignKeyField(
        "models.CargoCategory", 
        related_name="children", 
        null=True, 
        description="父分类"
    )
    level = fields.IntField(default=1, description="分类层级")
    sort_order = fields.IntField(default=0, description="排序")
    
    class Meta:
        table = "cargo_categories"
        table_description = "货物分类表"


class Cargo(BaseModel):
    """货物模型"""
    
    # 基本信息
    name = fields.CharField(max_length=200, description="货物名称")
    code = fields.CharField(max_length=100, unique=True, description="货物编码")
    barcode = fields.CharField(max_length=100, null=True, description="条形码")
    qr_code = fields.CharField(max_length=200, null=True, description="二维码")
    
    # 分类和属性
    category = fields.ForeignKeyField(
        "models.CargoCategory", 
        related_name="cargos", 
        description="货物分类"
    )
    brand = fields.CharField(max_length=100, null=True, description="品牌")
    model = fields.CharField(max_length=100, null=True, description="型号")
    specification = fields.TextField(null=True, description="规格说明")
    
    # 物理属性
    weight = fields.DecimalField(max_digits=10, decimal_places=3, null=True, description="重量(kg)")
    volume = fields.DecimalField(max_digits=10, decimal_places=3, null=True, description="体积(m³)")
    length = fields.DecimalField(max_digits=8, decimal_places=2, null=True, description="长度(cm)")
    width = fields.DecimalField(max_digits=8, decimal_places=2, null=True, description="宽度(cm)")
    height = fields.DecimalField(max_digits=8, decimal_places=2, null=True, description="高度(cm)")
    
    # 价值信息
    unit_price = fields.DecimalField(max_digits=12, decimal_places=2, null=True, description="单价")
    currency = fields.CharField(max_length=10, default="CNY", description="货币单位")
    
    # 状态信息
    status = fields.CharField(
        max_length=20, 
        default="in_stock",
        description="状态: in_stock-库存中, in_transit-运输中, delivered-已送达, damaged-损坏, lost-丢失"
    )
    
    # 存储信息
    warehouse = fields.ForeignKeyField(
        "models.Warehouse", 
        related_name="cargos", 
        null=True,
        description="所在仓库"
    )
    location = fields.CharField(max_length=100, null=True, description="具体位置")
    
    # 其他信息
    description = fields.TextField(null=True, description="货物描述")
    notes = fields.TextField(null=True, description="备注")
    
    class Meta:
        table = "cargos"
        table_description = "货物表"


class CargoImage(BaseModel):
    """货物图片模型"""
    
    cargo = fields.ForeignKeyField("models.Cargo", related_name="images", description="货物")
    image_url = fields.CharField(max_length=500, description="图片URL")
    image_type = fields.CharField(
        max_length=20, 
        default="product",
        description="图片类型: product-产品图, package-包装图, damage-损坏图"
    )
    description = fields.CharField(max_length=200, null=True, description="图片描述")
    sort_order = fields.IntField(default=0, description="排序")
    
    class Meta:
        table = "cargo_images"
        table_description = "货物图片表"


class CargoAttribute(BaseModel):
    """货物属性模型"""
    
    cargo = fields.ForeignKeyField("models.Cargo", related_name="attributes", description="货物")
    attribute_name = fields.CharField(max_length=100, description="属性名称")
    attribute_value = fields.TextField(description="属性值")
    attribute_type = fields.CharField(
        max_length=20, 
        default="text",
        description="属性类型: text-文本, number-数字, date-日期, boolean-布尔"
    )
    
    class Meta:
        table = "cargo_attributes"
        table_description = "货物属性表"
        unique_together = (("cargo", "attribute_name"),)
